package org.sourcecode.common.freemarker;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import net.sf.json.JSONObject;

public class TemplateProcesser {

	protected final Log log = LogFactory.getLog(getClass());

	protected Configuration configuration;

	public String processTpl2String(String tpl, Map<String, Object> model) {
		String content = "";
		try {
			Template template = configuration.getTemplate(tpl);
			content = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
		} catch (Exception e) {
			log.error("process tpl :" + tpl + "," + e.toString());
		}
		return content;
	}

	public String processTpl2StringWithJson(String tpl, JSONObject model) {
		String content = "";
		try {
			Template template = configuration.getTemplate(tpl);
			content = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
		} catch (Exception e) {
			log.error("process tpl :" + tpl + "," + e.toString());
		}
		return content;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

}
